package learnning.dao;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import learnning.dao.base.HibernateDao;
import learnning.entity.User;

public class UserDao extends HibernateDao {
	public void saveUser(String userName, String pass) {
		try {
			begin();
			User user = new User(userName, pass);
			getSession().save(user);
			commit();
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
		}
	}

	public User getUser(String name) {
		try {
			begin();
			Query q = getSession().createQuery("from User where name = :name");
			q.setString("name", name);
			User user = (User) q.uniqueResult();
			commit();
			return user;
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
			return null;
		}
	}

	public void deleteUser(User user) {
		try {
			begin();
			getSession().delete(user);
			commit();
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
		}
	}
}
